/*
 * thread_mutex.cpp
 * UNIX网络编程23.18
 *  Created on: 2008-8-12
 *      Author: root
 */
#include <stdio.h>
#include <pthread.h>

using namespace std;

#define NLOOP 50000
int counter;

pthread_mutex_t counter_mutex = PTHREAD_MUTEX_INITIALIZER;
void *doit(void *);

int main(int argc, char **argv)
{
	pthread_t tidA, tidB;
	pthread_create(&tidA, NULL, &doit, NULL);
	pthread_create(&tidB, NULL, &doit, NULL);

	pthread_join(tidA, NULL);
	pthread_join(tidB, NULL);
	return 0;
}

void *doit(void *vptr)
{
	int i, val;
	for (i = 0; i < NLOOP; i++)
	{
		pthread_mutex_lock(&counter_mutex);
		val = counter;
		printf("%u: %d\n", pthread_self(), val+1);
		counter = val + 1;
		pthread_mutex_unlock(&counter_mutex);
	}
	return NULL;
}
